package cn.fsw.pethome.pet.service.impl;

import cn.fsw.pethome.base.service.impl.BaseServiceImpl;
import cn.fsw.pethome.org.domain.Employee;
import cn.fsw.pethome.pet.domain.Pet;
import cn.fsw.pethome.pet.domain.PetDetail;
import cn.fsw.pethome.pet.domain.PetOnlineAuditLog;
import cn.fsw.pethome.pet.mapper.PetDetailMapper;
import cn.fsw.pethome.pet.mapper.PetMapper;
import cn.fsw.pethome.pet.mapper.PetOnlineAuditLogMapper;
import cn.fsw.pethome.pet.service.IPetService;
import cn.fsw.pethome.util.BaiduAuditUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fsw
 * @since 2022-10-23
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;


    @Override
    @Transactional
    public void add(Pet pet, Employee loginUser) {
        pet.setShopId(loginUser.getShop_id());
        petMapper.add(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.add(detail);
    }

    @Override
    @Transactional
    public void update(Pet pet, Employee employee) {
        PetDetail petDetail = new PetDetail();
        petDetail.setId(pet.getId());
        pet.setShopId(employee.getShop_id());
        petMapper.update(pet);
        petDetailMapper.update(pet.getDetail());
    }

    @Override
    @Transactional
    public void offsale(List<Long> ids) {
        Date date = new Date();
        petMapper.offsaleBatch(date,ids);
        List<PetOnlineAuditLog> list = new ArrayList<>();
        for (Long id : ids) {
            PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
            petOnlineAuditLog.setAuditTime(date);
            petOnlineAuditLog.setPetId(id);
            petOnlineAuditLog.setState(1);
            petOnlineAuditLog.setNote("下架成功");
            list.add(petOnlineAuditLog);
        }
        petOnlineAuditLogMapper.addBatch(list);

    }


    @Override
    public Map<String, Object> onsale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        List<String> names = new ArrayList<>();
        for (Long id : ids) {
            /*记录日志*/
            Date date = new Date();
            PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
            petOnlineAuditLog.setAuditTime(date);
            petOnlineAuditLog.setPetId(id);
            /*当前上架的物品*/
            Pet pet = petMapper.selectById(id);
            /*审核*/
            if(BaiduAuditUtils.TextCensor(pet.getName())){
                pet.setState(1L);
                pet.setOnsaletime(date);
                /*日志*/
                petOnlineAuditLog.setState(1);
                petOnlineAuditLog.setNote("审核成功");
                petMapper.update(pet);

            }else {
                /*日志*/
                petOnlineAuditLog.setState(0);
                petOnlineAuditLog.setNote("审核失败");
                names.add(pet.getName());
            }
            petOnlineAuditLogMapper.add(petOnlineAuditLog);
        }
        map.put("names",names);
        if(names.size() == 0){
            return null;
        }
        return map;
    }

    @Override
    public Pet selectByShopAndDetail(Long id) {
        return petMapper.selectByShopAndDetail(id);
    }


}
